#include "KmyMap.h"
#include "KmyWorld.h"

KmyMap::KmyMap()
{
}

KmyMap::KmyMap(int w, int h){
    map_W = w;
    map_H = h;
}

KmyMap::~KmyMap(){

}

void KmyMap::finalize(){
    ST_VectorNode *p = child_list.getHead();
    while (p!=NULL){
        KmyNode* node = (KmyNode*)(p->value);
        node->finalize();
        delete node;
        p = p->next;
    }
    child_list.clear();

//    p = sprites.getHead();
//    while(p!=NULL){
//        KmySprite *spt = (KmySprite*)p->value;
//        spt->finalize();
//        delete spt;
//        p = p->next;
//    }
//    sprites.clear();

    if (parent!=NULL){
        parent->removeChild(this);
    }
}

int KmyMap::getNodeType(){
    return KmyNode::NODE_TYPE_MAP;
}

void KmyMap::addSprite(KmySprite *spt){
    if (spt->id==0){
        spt->id = KmyNode::createID();
    }
    if (child_list.findValue((void*)spt)){
        return;
    }else{
        child_list.addValue((void*)spt);
    }
}

void KmyMap::removeSprite(KmySprite *spt){
    child_list.removeValue((void*)spt);
}


int KmyMap::getMapWidth(){
    return map_W;
}

int KmyMap::getMapHeight(){
    return map_H;
}

bool KmyMap::isSpriteInCamera(KmySprite *spt, St_Rect *camera){
    St_Rect mapcamera;
    mapcamera.w = camera->w;
    mapcamera.h = camera->h;
    mapcamera.x = (int)(camera->x*move_ratio_x);
    mapcamera.y = (int)(camera->y*move_ratio_y);
    return spt->isInCamera(&mapcamera);
}

void KmyMap::logic(St_Rect *camera){
    if (move_ratio_x==0 && move_ratio_y==0){
        ST_VectorNode *p = child_list.getHead();
        while(p!=NULL){
            KmyNode *spt = (KmyNode*)p->value;
            spt->logic();
            p = p->next;
        }
    }else{
        ST_VectorNode *p = child_list.getHead();
        while(p!=NULL){
            KmyNode *node = (KmyNode*)p->value;
            if (node->getNodeType() == KmyNode::NODE_TYPE_SPRITE){
                KmySprite *spt = (KmySprite*)node;
                if (spt->isInCamera(camera)){
                    spt->logic();
                }
            }else{
                node->logic();
            }
            p = p->next;
        }
    }
}

void KmyMap::render(KmyICanvas *screen){
    // tile render start

    //  tile render end

    ST_VectorNode *p = child_list.getHead();
    while(p!=NULL){
        KmyNode* node = (KmyNode*)p->value;
        if (node->getNodeType()==KmyNode::NODE_TYPE_SPRITE){
            KmySprite *sprite = (KmySprite*)node;
            if (sprite->isInScreen(screen)){
                sprite->render(screen);
            }
        }else{
            node->render(screen);
        }
        p = p->next;
    }
}

//void KmyMap::render(KmyICanvas* screen, St_Rect *camera){
//    St_Rect mapcamera;
//    mapcamera.w = camera->w;
//    mapcamera.h = camera->h;
//    mapcamera.x = (int)(camera->x*move_ratio_x);
//    mapcamera.y = (int)(camera->y*move_ratio_y);
//    ST_VectorNode *p = child_list.getHead();
//    while(p!=NULL){
//        KmyNode* node = (KmyNode*)p->value;
//        node->render(screen);
//        p = p->next;
//    }
//    p = sprites.getHead();
//    while(p!=NULL){
//        KmySprite *spt = (KmySprite*)p->value;
//        spt->render(screen, &mapcamera);
//        p = p->next;
//    }
//}

int KmyMap::getGlobalX(){
    if (parent!=NULL){
        if (parent->getNodeType() == KmyNode::NODE_TYPE_WORLD){
            KmyWorld *world = (KmyWorld*)parent;
            return -world->getCamera()->x*move_ratio_x;
        }else{
            return parent->getGlobalX()+x;
        }
    }else{
        return x;
    }
}

int KmyMap::getGlobalY(){
    if (parent!=NULL){
        if (parent->getNodeType() == KmyNode::NODE_TYPE_WORLD){
            KmyWorld *world = (KmyWorld*)parent;
            return -world->getCamera()->y*move_ratio_y;
        }else{
            return parent->getGlobalY()+y;
        }
    }else{
        return y;
    }
}
